package com.xieyong.mywebsite.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xieyong.mywebsite.model.entity.Notice;
import com.xieyong.mywebsite.mapper.BaseNoticeMapper;
import com.xieyong.mywebsite.service.BaseNoticeService;
import com.xieyong.mywebsite.util.TokenUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class BaseNoticeServiceImpl implements BaseNoticeService {

    @Resource
    private BaseNoticeMapper baseNoticeMapper;

    public Notice getById(Integer id) {
        return baseNoticeMapper.selectById(id);
    }

    public List<Notice> getAll() {
        return baseNoticeMapper.selectAll();
    }

    public PageInfo<Notice> getPage(Integer pageNum, Integer pageSize, Notice notice, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(pageNum,pageSize);
        List<Notice> notices = baseNoticeMapper.selectByCondition(notice.getTitle(),notice.getContent(),startTime,endTime);
        return PageInfo.of(notices);
    }

    public void add(Notice notice) {
        notice.setCreateTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
        baseNoticeMapper.insert(notice);
    }

    public void update(Notice notice) {
        notice.setUpdateTime(LocalDateTime.now());
        baseNoticeMapper.update(notice);
    }

    public void delete(Integer id) {
        baseNoticeMapper.delete(LocalDateTime.now(), TokenUtil.getCurrentUser().getId(),id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            baseNoticeMapper.delete(LocalDateTime.now(), TokenUtil.getCurrentUser().getId(),id);
        }
    }
}
